/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cl.alanis.dao.impl;

import cl.alanis.dao.Dao;
import cl.alanis.dao.StockDao;
import cl.alanis.dto.StockDto;
import cl.alanis.dto.VentaDto;
import cl.alanis.exception.StockNoExisteException;
import cl.alanis.util.AlanisLog;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;


/**
 *
 * @author Pía
 */
public class StockDaoImpl extends Dao implements StockDao {
    Logger logger=AlanisLog.obtenerLogger(StockDaoImpl.class);
    public StockDaoImpl(){
        super();
    }
    
    @Override
    public StockDto obtenerStock(StockDto stock) throws StockNoExisteException,Exception{
        logger.debug("Obteniendo stock:"+stock);
        StockDto stockRetorno = new StockDto();
        try {
            
            String consulta="SELECT COD_STOCK,"
                    + "COD_PROD,"
                    + "COD_VENTA,"
                    + "COD_INGRESO,"
                    + "COD_NOTAC,"
                    + "FECHA_INGRESO,"
                    + "DESCRIPCION_STOCK "
                    + "FROM STOCK WHERE COD_STOCK=?";
            PreparedStatement pstmt = conexion.preparedStatement(consulta);
            pstmt.setString(1, stock.getCodStock());
            ResultSet rs=pstmt.executeQuery();
            if(rs.next()){
                stockRetorno.setCodStock(rs.getString("COD_STOCK"));
                stockRetorno.setCodProducto(rs.getString("COD_PROD"));
                stockRetorno.setCodVenta(rs.getInt("COD_VENTA"));
                stockRetorno.setCodIngreso(rs.getInt("COD_INGRESO"));
                stockRetorno.setCodNotaCredito(rs.getInt("COD_NOTAC"));
                stockRetorno.setFechaIngreso(rs.getDate("FECHA_INGRESO").getTime());
                stockRetorno.setDescripcion(rs.getString("DESCRIPCION_STOCK"));
            }
            else{
                throw new StockNoExisteException("No existe el stock solicitado:"+stock.getCodStock());
            }
            
        } catch (SQLException ex) {
            logger.error("Error:"+ex.getMessage());
            throw new Exception("Error al obtener Stock");
        }
         logger.debug("Retorno:"+stockRetorno);
         return stockRetorno;
    }

    @Override
    public boolean actualizarStock(StockDto stock) throws Exception {
        logger.debug("Actualizando Stock:\n"+stock);
        String update="UPDATE STOCK SET COD_PROD=?,"+
                                       "COD_VENTA=?,"+
                                       "COD_INGRESO=?,"+
                                       "COD_NOTAC=?,"+
                                       "FECHA_INGRESO=?,"+
                                       "DESCRIPCION_STOCK=?"+
                        " WHERE COD_STOCK=?";
        try {
            PreparedStatement pstmt = conexion.preparedStatement(update);
            pstmt.setString(1, stock.getCodProducto());
            if(stock.getCodVenta() == null)
                pstmt.setNull(2, Types.INTEGER);
            else            
                pstmt.setInt(2, stock.getCodVenta());
            pstmt.setInt(3, stock.getCodIngreso());
            pstmt.setInt(4, stock.getCodNotaCredito());
            pstmt.setDate(5, new Date(stock.getFechaIngreso()));
            pstmt.setString(6, stock.getDescripcion());
            pstmt.setString(7, stock.getCodStock());
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            logger.error("Error:"+ex.getMessage());
            throw new Exception("Problema al actualizar Stock:"+stock);
        }
        return false;
    }
    
    public List obtenerStocks(VentaDto venta) throws Exception{
            logger.debug("Obteniendo stocks a partir de la venta:\n"+venta);
            List stocks = new ArrayList();
            if(venta.getCodVenta()==null)
                return stocks;
            StockDto stock=new StockDto();
            
            String consulta="SELECT COD_STOCK,"+
                                    "COD_PROD,"+
                                    "COD_VENTA,"+
                                    "COD_INGRESO,"+
                                    "COD_NOTAC,"+
                                    "FECHA_INGRESO,"+
                                    "DESCRIPCION_STOCK "+
                            "FROM STOCK "+
                            "WHERE COD_VENTA=?";
         try {
            PreparedStatement pstmt = conexion.preparedStatement(consulta);
            pstmt.setInt(1, venta.getCodVenta());
            ResultSet rs=pstmt.executeQuery();
            while(rs.next()){
                stock=new StockDto();
                stock.setCodStock(rs.getString("COD_STOCK"));
                stock.setCodProducto(rs.getString("COD_PROD"));
                stock.setCodVenta(rs.getInt("COD_VENTA"));
                stock.setCodIngreso(rs.getInt("COD_INGRESO"));
                stock.setCodNotaCredito(rs.getInt("COD_NOTAC"));
                stock.setFechaIngreso(rs.getDate("FECHA_INGRESO").getTime());
                stock.setDescripcion(rs.getString("DESCRIPCION_STOCK"));
                stocks.add(stock);
            }
            return stocks;
        } catch (SQLException ex) {
            logger.error("Error:"+ex.getMessage());
            throw new Exception("Error en el sistema");
        }
    }

    @Override
    public boolean insertarStock(StockDto stock) throws Exception{
        logger.debug("Insertando Stock:\n"+stock);
        String insert="INSERT INTO STOCK(COD_PROD,"+
                                    "COD_INGRESO,"+
                                    "FECHA_INGRESO) "
                                + "VALUES(?,?,?)";
        try {
            PreparedStatement pstmt = conexion.preparedStatement(insert);            
            pstmt.setString(1, stock.getCodProducto());
            pstmt.setInt(2, stock.getCodIngreso());
            pstmt.setDate(3, new Date(stock.getFechaIngreso()));
            pstmt.executeUpdate();
        } catch (SQLException ex) {
            logger.error("Error:"+ex.getMessage());
            throw new Exception("Error en el sistema");
        }
        return true;
    }
}
